﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCS.SqlSugar.Cmd.Models;
using SqlSugar;

namespace IceCS.SqlSugar.Cmd
{
    public class AppDbContext
    {
        private static string _connectionString = "Data Source=:memory:";
        private static string _connectionString2 = "Data Source=school.db";

        private static ConnectionConfig _connectionConfig = new ConnectionConfig()
        {
            ConnectionString = _connectionString2,
            //自动释放数据库，如果存在事务，在事务结束之后释放
            IsAutoCloseConnection = true,
            IndexSuffix = "idx_",
            DbType = DbType.Sqlite,
            //从实体特性中读取主键自增列信息   
            InitKeyType = InitKeyType.Attribute
        };

        public static void UseSqlSugarClient(Action<SqlSugarClient> action)
        {
            using (var client = new SqlSugarClient(_connectionConfig))
            {
                client.Aop.OnLogExecuting = (sql, pars) =>
                {
                    var logText = "sql:" + sql + "\r\nparams:" + client.Utilities.SerializeObject
                        (pars.ToDictionary(paramst => paramst.ParameterName, it => it.Value));
                    Console.WriteLine(logText);
                    Console.WriteLine();
                };
                action(client);
            }
        }

        public static void CreateTable(bool isBackup = false, int stringDefaultLength = 50, params Type[] types)
        {
            UseSqlSugarClient(client =>
            {
                client.CodeFirst.SetStringDefaultLength(stringDefaultLength);
                var path = Path.Combine(AppContext.BaseDirectory);
                //client.DbMaintenance.CreateDatabase(path);
                if (isBackup)
                {
                    client.CodeFirst.BackupTable().InitTables(types);
                }
                else
                {
                    client.CodeFirst.InitTables(types);
                }
            });

        }
        public static void CreateTables()
        {
            CreateTable(isBackup: false, stringDefaultLength: 50, typeof(Students));
            CreateTable(isBackup: false, stringDefaultLength: 50, typeof(Schools));
        }

        public static SimpleClient<Students> Student
        {
            get
            {
                SimpleClient<Students> simpleClient = null; ;
                UseSqlSugarClient(client =>
                {
                    simpleClient = new SimpleClient<Students>(client);
                });
                return simpleClient;
            }
        }
        public static SimpleClient<Schools> School
        {
            get
            {
                SimpleClient<Schools> simpleClient = null; ;
                UseSqlSugarClient(client =>
                {
                    simpleClient = new SimpleClient<Schools>(client);
                });
                return simpleClient;
            }
        }
    }
}
